package stateDiagram.EntityObjects;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Test;

import stateDiagram.figures.TransitionFigure;

public class SuperStateEntityTest {

	 private StateEntity testState;
	 private StateEntity testState2;
	 private SuperStateEntity ss1;
	 private SubStateEntity ss2;
	 private SubStateEntity ss3;
	 private String _name;
	 private ArrayList <TransitionEntity> incomingTransitions;
	 private ArrayList <TransitionEntity> outgoingTransitions;
	
	 private TransitionEntity t1;
	 private TransitionEntity t2;
	 private TransitionEntity t3;
	 private TransitionEntity t4;
	 private TransitionEntity t5;
	 
	 
	 //runs before each test 
	public void setup() 
	{
		_name = "testState";
		incomingTransitions = new ArrayList <TransitionEntity>();
		outgoingTransitions = new ArrayList <TransitionEntity>();
		
		testState = new StateEntity(_name, "default","default");
		ss1 =  new SuperStateEntity("s1","default","default");
		ss2 = new SubStateEntity("s2","default", "default");
		ss3 = new SubStateEntity("s3","default","default");
		t1 = new TransitionEntity("fromTest1",new TransitionFigure());
		t2 = new TransitionEntity("toTest2",new TransitionFigure());
		t3 = new TransitionEntity("toTest3",new TransitionFigure());
		t4 = new TransitionEntity("fromTest4",new TransitionFigure());
		t5 = new TransitionEntity("toSelf", new TransitionFigure());
	}
	//runs after each test
	public void tearDown()
	{
		
	}
	@Test
	public void testConstructor()
	{
		testState = new StateEntity(_name,"default","default");
		assertEquals(testState.getName(),_name);
	}
	@Test
	public void testSetName()
	{
		testState = new StateEntity(_name,"default","default");
		testState.setName("SillySassyStateSandwich");
		assertEquals(testState.getName(),"SillySassyStateSandwich");
		testState.setName("BluBluBlu");
		assertEquals(testState.getName(),"BluBluBlu");
		testState.setName(null);
		assertEquals(testState.getName(),"State");
	}
	
	@Test
	public void testAddOutgoingTransition()
	{
		testState = new StateEntity(_name, "default","default");
		t2 = new TransitionEntity("toTest2",new TransitionFigure());
		assertTrue(testState.addOutgoingTransition(t2));
		assertFalse(testState.addOutgoingTransition(null));
	}
	@Test
	public void testRemoveOutgoingTransition()
	{
		//TODO
		testState = new StateEntity(_name, "default","default");
		ss1 =  new SuperStateEntity("s1", "default","default");
		ss2 = new SubStateEntity("s2", "default","default");
		ss3 = new SubStateEntity("s3", "default","default");
		t1 = new TransitionEntity("fromTest1",new TransitionFigure());
		t2 = new TransitionEntity("toTest2",new TransitionFigure());
		t3 = new TransitionEntity("toTest3",new TransitionFigure());
		t4 = new TransitionEntity("fromTest4",new TransitionFigure());
		t5 = new TransitionEntity("toSelf",new TransitionFigure());
		testState.addOutgoingTransition(t1);
		testState.addOutgoingTransition(t2);
		assertTrue(testState.removeOutgoingTransition(t1)); //remove something that exists
		assertFalse(testState.removeOutgoingTransition(t1)); //remove something that does not exist
		assertFalse(testState.removeOutgoingTransition(null)); //remove null (there should never be null)
	}
	
	@Test
	public void testToXML()
	{
		//Without any transitions, only original name
		_name = "testState";
		testState = new SuperStateEntity(_name, "default","default1");
		assertTrue(testState.toXML().contains("<SuperStateName>testState</SuperStateName>"));
		assertTrue(testState.toXML().contains("<Action>default</Action>"));
		assertTrue(testState.toXML().contains("<Action>default1</Action>"));
		//assertTrue(testState.toXML().contains("<>"));
		assertTrue(testState.toXML().contains("</ExitAction><SubStates>"));
		
		//test with outgoing transitions
		StateEntity testing = new StateEntity("testing", "default","default");
		TransitionEntity entitytest = new TransitionEntity("happy",new TransitionFigure());
		entitytest.setFromState(testing);
		entitytest.setToState(testState);
		testing.addOutgoingTransition(entitytest);
		assertTrue(entitytest.toXML().contains("<Transition>"));
		assertTrue(testing.toXML().contains("<TransitionEvent>happy</TransitionEvent>"));
		
		
		
		//With a transition to itself
		testState2 = new StateEntity("testState", "default","default"); //remove transitions without calling removeTransition
		
		t5 = new TransitionEntity("toSelf",new TransitionFigure());
		t5.setFromState(testState2);
		t5.setToState(ss2);
		testState2.addOutgoingTransition(t5);
		assertTrue(testState2.toXML().contains("<TransitionEvent>toSelf</TransitionEvent>"));
		
		
		//name is empty
		testState2.setName(null);
		assertTrue(testState2.getName().equals("State"));// attempting to set name to null should produce default name
		testState2.setName("");
		assertTrue(testState2.getName().equals("State"));
		//should not be allowed should be set to default string.
	}
}
